$(function(){
    //跳转登录、注册页面
    $('#link_reg,#link_login').click(function(){
        $(this).parents('form').hide().siblings('form').show();
    });
    //自定义校验规则
    layui.form.verify({
        pwd:[/^\S{6,12}$/,'密码为6-12位非空格'],
        repwd:function(value){
            var pwd = $('#reg_form [name="password"]').val();
            if (pwd != value){
                return '密码不一致';
            }
        }
    });
    //注册页面
    $('#reg_form').on('submit',function(e){
        e.preventDefault();
        var username = $('#reg_form [name="username"]').val();
        var password = $('#reg_form [name="password"]').val();
        $.post('/api/reguser',{username:username,password:password},function(res){
            if (res.status != 0)return layer.msg(res.message);
            layer.msg(res.message);
            $('#link_login').click();
        })
    });
    //登录页面
    $('#login_form').on('submit',function(e){
        e.preventDefault();
        $.ajax({
            type:'post',
            url:'/api/login',
            data:$('#login_form').serialize(),
            success:function(res){
                if (res.status != 0)return layer.msg(res.message);
                layer.msg(res.message);
                localStorage.setItem('token',res.token);
                location.href = '/index.html'
            }
        })
    })



})